﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Design;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace ToddMcDermid.SSIS
{
    public partial class HTMLTableDestinationEditor : Form
    {
        #region Private Variables
        private IDTSComponentMetaData100 _componentMetaData;
        private Variables _variables;
        private bool _loaded;
        struct ColumnOrder : IEquatable<ColumnOrder>
        {
            public string Name;
            public int LineageID;
            public bool Selected;

            public override string ToString()
            {
                return this.Name + " (" + this.LineageID.ToString() + ")";
            }

            #region IEquatable<ColumnOrder> Members
            bool IEquatable<ColumnOrder>.Equals(ColumnOrder other)
            {
                return this.LineageID.Equals(other.LineageID);
            }
            #endregion
        }
        private List<ColumnOrder> _columnOrder;
        #endregion

        #region Constructor
        public HTMLTableDestinationEditor(IDTSComponentMetaData100 componentMetaData, Variables vars)
        {
            InitializeComponent();
            this._componentMetaData = componentMetaData;
            this._variables = vars;
            this._loaded = false;
            this._columnOrder = new List<ColumnOrder>();

            IDTSCustomProperty100 columnOrderProperty = HTMLTableDestination.GetColumnOrderProperty(this._componentMetaData);
            int[] columnOrderLineages = (int[])columnOrderProperty.Value;
            IDTSVirtualInput100 vInput = this._componentMetaData.InputCollection[0].GetVirtualInput();
            foreach (int lineageID in columnOrderLineages)
            {
                foreach (IDTSVirtualInputColumn100 vInputColumn in vInput.VirtualInputColumnCollection)
                {
                    if (vInputColumn.LineageID == lineageID)
                    {
                        ColumnOrder columnOrder;
                        columnOrder.Name = vInputColumn.Name;
                        columnOrder.LineageID = vInputColumn.LineageID;
                        columnOrder.Selected = true;
                        this._columnOrder.Add(columnOrder);
                        break;
                    }
                }
            }
            foreach (IDTSVirtualInputColumn100 vInputColumn in vInput.VirtualInputColumnCollection)
            {
                ColumnOrder columnOrder;
                columnOrder.Name = vInputColumn.Name;
                columnOrder.LineageID = vInputColumn.LineageID;
                columnOrder.Selected = false;

                if (!this._columnOrder.Contains(columnOrder))
                {
                    this._columnOrder.Add(columnOrder);
                }
            }
        }
        #endregion

        #region Form Events
        #region Load
        private void HTMLTableDestinationEditor_Load(object sender, EventArgs e)
        {
            #region Output Table Variable
            this.cboOutputTableVariable.Items.Clear();
            foreach (Variable variable in this._variables)
            {
                if ((!variable.SystemVariable) && (variable.DataType == TypeCode.String))
                {
                    this.cboOutputTableVariable.Items.Add(variable.QualifiedName);
                }
            }
            IDTSCustomProperty100 outputVariableProperty = HTMLTableDestination.GetOutputVariableProperty(this._componentMetaData);
            if (this._variables.Contains((string)outputVariableProperty.Value))
            {
                this.cboOutputTableVariable.SelectedItem = (string)outputVariableProperty.Value;
            }
            #endregion

            #region Output Row Count Variable
            this.cboOutputRowCountVariable.Items.Clear();
            this.chkOutputRowCountVariable.Checked = false;
            this.cboOutputRowCountVariable.Enabled = false;
            foreach (Variable variable in this._variables)
            {
                if ((!variable.SystemVariable) 
                    && ((variable.DataType == TypeCode.Int32)
                        || (variable.DataType == TypeCode.Int64)))
                {
                    this.cboOutputRowCountVariable.Items.Add(variable.QualifiedName);
                }
            }
            IDTSCustomProperty100 rowcountVariableProperty = HTMLTableDestination.GetRowCountVariableProperty(this._componentMetaData);
            if (this._variables.Contains((string)rowcountVariableProperty.Value))
            {
                this.cboOutputRowCountVariable.SelectedItem = (string)rowcountVariableProperty.Value;
                this.chkOutputRowCountVariable.Checked = true;
                this.cboOutputRowCountVariable.Enabled = true;
            }
            #endregion

            #region Row Colour Column
            this.cboRowColourColumn.Items.Clear();
            this.chkRowColourColumn.Checked = false;
            this.cboRowColourColumn.Enabled = false;
            foreach (IDTSVirtualInputColumn100 vInputColumn in this._componentMetaData.InputCollection[0].GetVirtualInput().VirtualInputColumnCollection)
            {
                if ((vInputColumn.DataType == Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_STR)
                    || (vInputColumn.DataType == Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR))
                {
                    this.cboRowColourColumn.Items.Add(vInputColumn.Name);
                }
            }
            IDTSCustomProperty100 rowColourProperty = HTMLTableDestination.GetRowColourColumnProperty(this._componentMetaData);
            if (((string)rowColourProperty.Value) != "")
            {
                this.cboRowColourColumn.SelectedItem = (string)rowColourProperty.Value;
                this.chkRowColourColumn.Checked = true;
                this.cboRowColourColumn.Enabled = true;
            }
            #endregion

            #region Line Breaks
            IDTSCustomProperty100 lineBreakProperty = HTMLTableDestination.GetReplaceNewlineWithBreakProperty(this._componentMetaData);
            this.chkReplaceNewlinesWithBreaks.Checked = (bool)lineBreakProperty.Value;
            #endregion

            #region Selected Columns
            this.chkSelectedColumns.Items.Clear();
            foreach (ColumnOrder columnOrder in this._columnOrder)
            {
                this.chkSelectedColumns.Items.Add(columnOrder, columnOrder.Selected);
            }
            #endregion

            this._loaded = true;
        }
        #endregion
        #endregion

        #region Control Events
        #region Interactivity
        private void chkRowColourColumn_CheckedChanged(object sender, EventArgs e)
        {
            if (this._loaded)
            {
                this.cboRowColourColumn.Enabled = this.chkRowColourColumn.Checked;
            }
        }

        private void chkOutputRowCountVariable_CheckedChanged(object sender, EventArgs e)
        {
            if (this._loaded)
            {
                this.cboOutputRowCountVariable.Enabled = this.chkOutputRowCountVariable.Checked;
            }
        }

        private void btnMoveSelectedColumnUp_Click(object sender, EventArgs e)
        {
            ColumnOrder selectedColumnOrder = (ColumnOrder)this.chkSelectedColumns.SelectedItem;
            int oldPosition = this.chkSelectedColumns.SelectedIndex;
            bool selected = this.chkSelectedColumns.CheckedIndices.Contains(oldPosition);
            this.chkSelectedColumns.Items.RemoveAt(oldPosition);
            this.chkSelectedColumns.Items.Insert(oldPosition - 1, selectedColumnOrder);
            this.chkSelectedColumns.SetItemChecked(oldPosition - 1, selected);
            this.chkSelectedColumns.SelectedIndex = oldPosition - 1;
        }

        private void btnMoveSelectedColumnDown_Click(object sender, EventArgs e)
        {
            ColumnOrder selectedColumnOrder = (ColumnOrder)this.chkSelectedColumns.SelectedItem;
            int oldPosition = this.chkSelectedColumns.SelectedIndex;
            bool selected = this.chkSelectedColumns.CheckedIndices.Contains(oldPosition);
            this.chkSelectedColumns.Items.RemoveAt(oldPosition);
            this.chkSelectedColumns.Items.Insert(oldPosition + 1, selectedColumnOrder);
            this.chkSelectedColumns.SetItemChecked(oldPosition + 1, selected);
            this.chkSelectedColumns.SelectedIndex = oldPosition + 1;
        }
        #endregion

        #region OK
        private void cmdOK_Click(object sender, EventArgs e)
        {
            #region Output Table Variable
            IDTSCustomProperty100 outputVariableProperty = HTMLTableDestination.GetOutputVariableProperty(this._componentMetaData);
            if (this.cboOutputTableVariable.SelectedIndex >= 0)
            {
                outputVariableProperty.Value = this.cboOutputTableVariable.SelectedItem;
            }
            else
            {
                outputVariableProperty.Value = "";
            }
            #endregion

            #region Output Row Count Variable
            IDTSCustomProperty100 rowcountVariableProperty = HTMLTableDestination.GetRowCountVariableProperty(this._componentMetaData);
            if (this.chkOutputRowCountVariable.Checked && (this.cboOutputRowCountVariable.SelectedIndex >= 0))
            {
                rowcountVariableProperty.Value = this.cboOutputRowCountVariable.SelectedItem;
            }
            else
            {
                rowcountVariableProperty.Value = "";
            }
            #endregion

            #region Row Colour Column
            IDTSCustomProperty100 rowColourProperty = HTMLTableDestination.GetRowColourColumnProperty(this._componentMetaData);
            if (this.chkRowColourColumn.Checked && (this.cboRowColourColumn.SelectedIndex >= 0))
            {
                rowColourProperty.Value = this.cboRowColourColumn.SelectedItem;
            }
            else
            {
                rowColourProperty.Value = "";
            }
            #endregion

            #region Line Breaks
            IDTSCustomProperty100 lineBreakProperty = HTMLTableDestination.GetReplaceNewlineWithBreakProperty(this._componentMetaData);
            lineBreakProperty.Value = this.chkReplaceNewlinesWithBreaks.Checked;
            #endregion

            #region Column Selection
            int[] columnOrderLineages = new int[this.chkSelectedColumns.CheckedItems.Count];
            for (int index = 0; index < this.chkSelectedColumns.CheckedItems.Count; index++)
            {
                columnOrderLineages[index] = ((ColumnOrder)this.chkSelectedColumns.CheckedItems[index]).LineageID;
            }
            IDTSCustomProperty100 columnOrderProperty = HTMLTableDestination.GetColumnOrderProperty(this._componentMetaData);
            columnOrderProperty.Value = columnOrderLineages;
            #endregion

            HTMLTableDestination.ReinitializeMetaData(this._componentMetaData);
            this.Close();
        }
        #endregion

        #region Close
        private void cmdClose_Click(object sender, EventArgs e)
        {

        }
        #endregion
        #endregion
    }
}
